package com.collaboration.electron.controllers;

import com.collaboration.electron.entities.*;
import com.collaboration.electron.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author pinzi
 * @param
 * @return 无
 */
@Controller
@RequestMapping("save")
public class AddController {

    @Autowired
    UserService userService;

    @Autowired
    ProductService productService;

    @Autowired
    CompositionService compositionService;

    @Autowired
    DetailedCompositionService detailedCompositionService;

    @Autowired
    BrandService brandService;

    @Autowired
    ClassificationService classificationService;

    @GetMapping("")
    public String displaySave(Model model, Principal principal) {

//        String name = principal.getName();
//        Player player = playerService.findPlayerByUserName(name);
//        long Id = player.getUserId();

        String name = principal.getName();
        model.addAttribute("name",name);

        U u = userService.findByUserName(name);
        long Id = u.getUserId();
        model.addAttribute("Id",Id);

        U user = new U();
        model.addAttribute("U", user);

        Product product = new Product();
        model.addAttribute("product",product);

        List<Product> products = productService.getAll();
        model.addAttribute("products", products);

        Composition composition = new Composition();
        model.addAttribute("composition", composition);

        DetailedComposition detailedComposition = new DetailedComposition();
        model.addAttribute("detailedComposition", detailedComposition);

        Brand brand = new Brand();
        model.addAttribute("brand", brand);

        Classification classification = new Classification();
        model.addAttribute("classification", classification);

        return "add";
    }




    @PostMapping("saveAddProduct")
    public String saveAddProduct(Model model,
                                 Product product,
                                 Classification classification,
                                 Brand brand,
                                 U user,
                                 Principal principal){

        String name = principal.getName();
        U u = userService.findByUserName(name);
        long Id = u.getUserId();


        product.setUserId(Id);
        product.setBrand(brand);
        product.setClassification(classification);
        productService.save(product);

        return "redirect:/save";
    }

    @PostMapping("saveAddComposition")
    public String saveAddComposition(Model model,
                                     Product product,
                                     Composition composition,
                                     DetailedComposition detailedComposition,
                                     Principal principal){
        String name = principal.getName();
        U u = userService.findByUserName(name);
        long Id = u.getUserId();



        composition.setDetailedComposition(detailedComposition);

        detailedComposition.setProduct(product);

        compositionService.save(composition);
        detailedCompositionService.save(detailedComposition);


        return "redirect:/save";
    }



}